package com.ruoyi.filter;
import oshi.SystemInfo;

import oshi.util.FormatUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.utils.JwtUtils;
import com.ruoyi.utils.MyThreadLocal;
import io.jsonwebtoken.Claims;
import org.apache.http.HttpRequest;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.NetworkIF;
import oshi.software.os.NetworkParams;
import oshi.software.os.OperatingSystem;
import oshi.util.FormatUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.SQLOutput;
import java.util.*;


@WebFilter(urlPatterns = "/front/*")
public class FrontFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        
        String requestURI = httpServletRequest.getRequestURI();
        System.out.println("requestURI = " + requestURI);
//        url包含login就直接放行
        if (requestURI.contains("login")||requestURI.contains("register")) {
            System.out.println("login");
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        String token = httpServletRequest.getHeader("Authorization");
        System.err.println("token = " + token);
        //解析错误或为空
        if (token == null|| token.equals("") ) {
            AjaxResult ajaxResult = AjaxResult.error("NOT_LOGIN");
            PrintWriter writer = servletResponse.getWriter();
            writer.write(JSONObject.toJSONString(ajaxResult));
            return;
        }
        Claims claims = JwtUtils.validateToken(token);
        System.out.println("claims = " + claims);
        if (claims == null){
            AjaxResult ajaxResult = AjaxResult.error("NOT_LOGIN");
            PrintWriter writer = servletResponse.getWriter();
            writer.write(JSONObject.toJSONString(ajaxResult));
            return ;
        }
        //解析成功放行
        //全局设置id
        MyThreadLocal.setData((Integer) claims.get("id"));
        filterChain.doFilter(servletRequest,servletResponse);
    }

    public static void main(String[] args) {

        HashMap<String, String> map = new HashMap<>();
        map.put("name","yws");
        map.put("age","18");
        map.put("sex","男");
        map.put("hobby","football");
        map.put("address","上海");
        map.put("phone","12345678901");
        map.put("email","12345678901@163.com");


        Iterator<Map.Entry<String, String>> iterator =  map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> entry = iterator.next();
            String key = entry.getKey();
            if (key.equals("age") && entry.getValue().equals("18")){
                iterator.remove();
            }
        }

        System.out.println(map);
        }
    }








